﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OrderForm
{
    public partial class CreateForm : Form
    {
        readonly Order.OrderService service;
        readonly List<Order.Commodity> commodities;
        public CreateForm(Order.OrderService o)
        {
            service = o;
            InitializeComponent();
            commodities = new List<Order.Commodity>();
        }

        private void CreateDetail_Click(object sender, EventArgs e)
        {
            var commodity = new Order.Commodity()
            {
                Name = comName.Text,
                Price = (double)comPrice.Value,
                Amount = (int)comCount.Value
            };
            commodities.Add(commodity);
        }

        private void Create_Click(object sender, EventArgs e)
        {
            try
            {
                service.InquiryOrders(ID.Text, Order.OrderService.InquiryType.OrderID);
                MessageBox.Show(this, "订单号重复。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                return;
            }
            catch { }
            Order.Order order = new Order.Order(ID.Text);
            order.Customer = new Order.Customer() { Name = customer.Text };
            order.OrderTime = calendar.SelectionStart;
            order.OrderDetails = new Order.OrderDetail[1];
            order.OrderDetails[0] = new Order.OrderDetail();
            order.OrderDetails[0].Commodities = commodities.ToArray();
            order.ComputeTotal();
            service.AddOrder(order);
            Close();
        }

    }
}
